﻿using ProjectManager.Interface;
using ProjectManager.Logic;
using ProjectManager.Logic.Content;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManager.Controller
{
    internal partial class LogicController : ILogic
    {
        public void CreateProject(string url, string selfName, string gitAccount, string password)
        {
            ShowWait?.Invoke("正在创建项目中...");
            //Task.Run(() =>
            //{
            //    try
            //    {

            // 第一步初始化项目信息
            localFileController.GenerateProjectPath(url, out string rootPath, out string taskPath, out string memberPath);
            ProjectInformation projectInformation = new()
            {
                Url = url,
                Name = GetNameFromGitUrl(url),
                Path = rootPath,
                TaskPath = taskPath,
                MemberPath = memberPath
            };
            // 第二步初始化项目中的个人信息
            MemberInformation memberInformation = new()
            {
                ID = 1,
                Name = selfName
            };
            // 第三步切换当前项目
            CurrentProject = projectInformation;
            CurrentIdentity = memberInformation;
            // 第七步更新本地配置
            localFileController.UpdateProject(projectInformation, memberInformation, gitAccount, password);
            localFileController.UpdateConfig(new UserConfig()
            {
                CurrentProject = this.CurrentProject,
                CurrentIdenity = this.CurrentIdentity,
            });
            // 第四步新建主线任务
            TeamInformation teamInformation = new()
            {
                ID = 1,
                Name = selfName,
                Leader = memberInformation,
                Members = [],
                Tasks = [],
                IsIndividual = true,
                IsCancelled = false
            };
            string masterPath = Path.Combine(CurrentProject.Path, "master");
            Directory.CreateDirectory(masterPath);
            ProjectTaskInformation projectTaskInformation = new()
            {
                ID = 1,
                TeamID = teamInformation.ID,
                BranchName = "master",
                Path = masterPath,
                Name = projectInformation.Name,
                Description = "主线任务",
                protectedPaths = ["/"],
                StartDate = DateTime.Now,
                EndDate = DateTime.Now + TimeSpan.FromDays(30),
                Status = ProjectTaskStatus.Working,
                ChildTask = [],
            };
            TeamInformation mainTeamInformation = new()
            {
                ID = 2,
                Name = projectInformation.Name,
                Leader = memberInformation,
                Members = [],
                Tasks = [projectTaskInformation],
                IsIndividual = false,
                IsCancelled = false
            };
            gitController.Clone(projectInformation.Url, masterPath);
            // 第五步创建任务分支和团队分支
            gitController.CreateBranch(masterPath, "Task");
            gitController.Clone(projectInformation.Url, projectInformation.TaskPath, "Task");
            gitController.CreateBranch(masterPath, "Member");
            gitController.Clone(projectInformation.Url, projectInformation.MemberPath, "Member");
            // 第六步初始化最初的任务和团队
            sqlController.AddTask(projectInformation, projectTaskInformation);
            gitController.Commit(projectInformation.TaskPath, "Task"
                , "Init Main Task", "Init Main Task");
            gitController.Push(projectInformation.TaskPath, "Task");

            sqlController.AddTeam(projectInformation, teamInformation);
            sqlController.AddTeam(projectInformation, mainTeamInformation);
            gitController.Commit(projectInformation.MemberPath, "Member"
                , "Init Main Team", "Init Main Team");
            gitController.Push(projectInformation.MemberPath, "Member");

            EndWait?.Invoke();
            //    }
            //    catch (Exception ex)
            //    {
            //        EndWait?.Invoke();
            //        ShowErrorMessage?.Invoke("git无法创建项目" + ex.Message);
            //    }
            //});
        }
        public void JoinProject(string url, string selfName, string gitAccount, string password)
        {
            ShowWait?.Invoke("正在加入项目中...");
            //Task.Run(() =>
            //{
            //    try
            //    {
            // 第一步初始化项目信息
            localFileController.GenerateProjectPath(url, out string rootPath, out string taskPath, out string memberPath);
            ProjectInformation projectInformation = new()
            {
                Url = url,
                Name = GetNameFromGitUrl(url),
                Path = rootPath,
                TaskPath = taskPath,
                MemberPath = memberPath
            };
            // 第二步将任务分支和成员分支clone到本地
            gitController.Clone(projectInformation.Url, projectInformation.TaskPath, "Task");
            gitController.Clone(projectInformation.Url, projectInformation.MemberPath, "Member");
            // 第三步获取所有团队
            var teams = sqlController.GetTeams(projectInformation);
            // 第四步创建自己的身份
            int selfID = teams.GenerateMemberID();
            MemberInformation memberInformation = new()
            {
                ID = selfID,
                Name = selfName,
            };
            int teamID = teams.GenerateTeamID();
            TeamInformation teamInformation = new()
            {
                ID = teamID,
                Name = memberInformation.Name,
                Leader = memberInformation,
                Members = [],
                Tasks = [],
                IsIndividual = true,
                IsCancelled = false
            };
            // 第五步将身份信息添加到本地
            localFileController.UpdateProject(projectInformation, memberInformation, gitAccount, password);
            CurrentProject = projectInformation;
            CurrentIdentity = memberInformation;
            localFileController.UpdateConfig(new()
            {
                CurrentProject = this.CurrentProject,
                CurrentIdenity = this.CurrentIdentity,
            });

            sqlController.AddTeam(projectInformation, teamInformation);
            gitController.Commit(projectInformation.MemberPath, "Member"
                , $"{selfName} Join Project", $"{selfName} Join Project");
            EndWait?.Invoke();
            //}
            //    catch (Exception ex)
            //    {
            //        EndWait?.Invoke();
            //        ShowErrorMessage?.Invoke(ex.Message);
            //    }
            //});
        }
        public List<(ProjectInformation, MemberInformation)> GetProjectList()
        {
            return localFileController.GetProjectList();
        }
    }
}
